package cn.fx.framework.redis;

import org.springframework.cache.interceptor.KeyGenerator;

import java.lang.reflect.Method;

/**
 * 缓存key生成器：对象名:方法名:对象名
 * @author ljh
 * @date 2018-08-18 21:29
 */
public class MyKeyGenerator implements KeyGenerator {
    @Override
    public Object generate(Object o, Method method, Object... objects) {
        StringBuilder sb = new StringBuilder(32);
        sb.append(o.getClass().getName());
        sb.append(":");
        sb.append(method.getName());
        if (objects.length > 0) {
            sb.append("#");
            String sp = "";
            for (Object object : objects) {
                sb.append(sp);
                if (object == null) {
                    sb.append("NULL");
                } else {
                    sb.append(object.toString());
                }
                sp = ".";
            }
        }
        return sb.toString();
    }
}
